﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheccCoverplateCulvertAssemble.util
{
    public class StationConvert
    {
        /// <summary>
        /// 将数字转为桩号
        /// </summary>
        /// <param name="zhName"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public static string ConvertZH(string prefix, double station)
        {
            string stake = "";
            bool pd = true;

            if (station >= 0)
            {
                pd = true;
            }
            else
            {
                pd = false;
                station = Math.Abs(station);
            }


            if (station < 10)
            {
                stake = "0" + "+" + "00" + station;
            }
            else if (station < 100 && station >= 10)
            {
                stake = "0" + "+" + "0" + station;
            }
            else if (station >= 100 && station < 1000)
            {
                stake = "0" + "+" + station;
            }
            else
            {
                int temp = Convert.ToInt32(station);
                int digit = temp / 1000;
                double remainder = Math.Round(station - (digit * 1000), 3);
                if (remainder < 10)
                {
                    stake = digit + "+" + "00" + remainder;
                }
                else if (remainder >= 10 && remainder < 100)
                {
                    stake = digit + "+" + "0" + remainder;
                }
                else
                {
                    stake = digit + "+" + remainder;
                }
            }
            if (pd == false)
            {
                stake = prefix + "-" + stake;
            }
            else
            {
                stake = prefix + stake;
            }

            return stake;
        }

        /// <summary>
        /// 将实际桩号抓换为数字桩号
        /// </summary>
        /// <param name="str"></param>
        /// <param name="ZH"></param>
        /// <returns></returns>
        public static double RealZH(string str, string ZH)
        {
            if (str == null) return 0;
            else str.Trim();

            str = str.Replace(ZH, "");

            str = System.Text.RegularExpressions.Regex.Replace(str, @"[A-Za-z]+", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            str = System.Text.RegularExpressions.Regex.Replace(str, "[+]", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            if (str.ToCharArray()[0] == '0' && str.ToCharArray().Length > 2)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    if (str.First<char>() == '0')
                        str = str.Substring(1, str.Length - 1);
                    else
                        break;
                }

            }

            double n = Convert.ToDouble(str);
            return n;
        }
    }
}
